﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace PlantsZombies.Common.Bullet
{
    [PlantZombie(SpeciesType.SunWuKong)]
    public class SunWuKongBullet : BulletObject
    {
        public SunWuKongBullet(ImageObject[] images, GameObject gameObject)
             : base(new Size(110, 110), images, gameObject)//40, 40
        {

        }
        public override unsafe void Move(ListGameObject* ptr)
        {
            for (int i = 0; i < _bullets.Count; i++)
            {
                var item = _bullets[i];
                if (item.Index >= _imagesBullet.Length)
                    _bullets.RemoveAt(i);
            }

            if (_gameObject.CurPlayIndex == 15)
            {
                for (int i = 0; i < _gameObject.MaxRow; i++)
                {
                    for (int j = 0; j < _gameObject.UseMaxColumn; j++)
                    {
                        var p = ptr + i * _gameObject.MaxColumn + j;
                        if (p->Game.Count == 0) continue;
                        for (int k = 0; k < p->Game.Count; k++)
                        {
                            var game = p->Game[k];
                            if (!game.IsZombie() || game.Column > _gameObject.UseMaxColumn) continue;

                            BulletPoint bulletPoint = null;
                            for (int m = 0; m < _bullets.Count; m++)
                            {
                                var b = _bullets[m];
                                if (b.Id == game.Id)
                                {
                                    bulletPoint = b;
                                    break;
                                }
                            }
                            if (bulletPoint != null)
                            {
                                bulletPoint.X = game.Location.X - BulletSize.Width / 2;
                                bulletPoint.Y = game.Location.Y - BulletSize.Height / 2;
                                bulletPoint.Row = game.Row;
                                bulletPoint.Col = game.Column;
                            }
                            else
                            {
                                _bullets.Add(new BulletPoint
                                {
                                    Index = 0,
                                    Id = game.Id,
                                    X = game.Location.X - BulletSize.Width / 2,
                                    Y = game.Location.Y - BulletSize.Height / 2,
                                    Row = game.Row,
                                    Col = game.Column
                                });
                            }
                        }
                    }
                }
            }
        }
        public override void Play(IntPtr hdc)
        {
            for (int i = 0; i < _bullets.Count; i++)
            {
                var item = _bullets[i];
                if (item.Index >= _imagesBullet.Length) continue;
                var img = _imagesBullet[item.Index];
                GDI32.DrawImageTransparenc(hdc, img.BitmapPtr, item.X, item.Y, img.Width, img.Height, BulletSize.Width, BulletSize.Height);
                ++item.Index;
            }
        }
        protected override unsafe void CheckAttack(ListGameObject* ptr)
        {
            for (int i = _bullets.Count - 1; i >= 0; i--)
            {
                var item = _bullets[i];
                if (item.Index != 8) continue;

                var p = ptr + item.Row * _gameObject.MaxColumn + item.Col;
                if (*p == null || p->Game.Count == 0) continue;
                var games = p->Game;
                for (int j = 0; j < p->Game.Count; j++)
                {
                    var game = p->Game[j];
                    if (game == null) continue;
                    if (!game.IsZombie()) continue;
                    if ((game.Life -= _gameObject.Damage) <= 0)
                        p->Game.RemoveAt(j);
                }
            }
        }
    }
}
